<?php
include("student_lock.php");
?>

<?Php
$username=$_SESSION['login_user'];

if($_SERVER["REQUEST_METHOD"] == "POST")
{
	$address1=addslashes($_POST['address1']);
	$address2=addslashes($_POST['address2']);
	$city=addslashes($_POST['city']);
	$state=addslashes($_POST['state']);
	$zip=addslashes($_POST['zip']);
	$homePhone=addslashes($_POST['homePhone']);
	$workPhone=addslashes($_POST['workPhone']);
	$cellPhone=addslashes($_POST['cellPhone']);
	$email=addslashes($_POST['email']);
	
	if(isset($_POST['contactInfo']))
	{
		$updateInfoQuery="UPDATE student SET address1=$address1, address2=$address2, city=$city, state=$state, zip=$zip, home_phone=$homePhone, work_phone=$workPhone, cell_phone=$cellPhone";
		$updateResult=mysql_query($updateInfoQuery);
		echo "BOOM";
	}
	else if(isset($_POST['changePassword']))
	{
		$row=mysql_fetch_row(mysql_query("SELECT password FROM user WHERE id='$username'"));
		
		if($row[0]!=$_POST['oldPassword'])
		{
			$passwordError="The old password you enterred is incorrect.";
		}
		else if($_POST['newPassword']!=$_POST['repeatPassword'])
		{
			$passwordError="Your passwords didn't match.";
		}
		
		if(!isset($passwordError))
		{
			$newPassword=addslashes($_POST['newPassword']);
			
			$updateInfoQuery="UPDATE student SET address1=$address1, address2=$address2, city=$city, state=$state, zip=$zip, home_phone=$homePhone, work_phone=$workPhone, cell_phone=$cellPhone";
			$updateResult=mysql_query($updateInfoQuery);
			
			$updatePasswordQuery="UPDATE user SET password=$newPassword where id=$username";
			$updatePasswordResult=mysql_query($updatePasswordQuery);
		}
		
	}
}

	$username=$_SESSION['login_user'];
	//get student info
	$studentQuery="SELECT * FROM student WHERE id='$username'";
	$studentRow=mysql_fetch_row(mysql_query($studentQuery));

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>View Profile</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<table align="center">
<tr><td width="200">Welcome <?php echo $_SESSION['login_user']; ?></td>
<td width="400" align="center"><h1>Profile</h1></h1></td>
<td width="200" align="right"><a href="logout.php">Logout</a></td>
</tr>
</table>

<form action="view_profile.php" method="post">
<table width="800" align="center">
<tr><td align="center" colspan="3"><b>Contact Information</b></td></tr>
<tr><td width="50">Address:</td></tr>
<tr><td><input name="text" type="text" id="address1" width="250" value="<?php echo $studentRow[4]; ?>"></td></tr>
<tr><td><input name="text" type="text" id="address2" width="250" value="<?php echo $studentRow[5]; ?>"></td></tr>
<tr>
<td>City:</td>
<td>State:</td>
<td>Zip:</td>
</tr>
<tr><td><input name="text" type="text" id="city" width="250" value="<?php echo $studentRow[6]; ?>"></td>
<td><input name="text" type="text" id="state" width="250" value="<?php echo $studentRow[7]; ?>"></td>
<td><input name="text" type="text" id="zip" width="250" value="<?php echo $studentRow[8]; ?>"></td>
</tr>
<tr>
<td>Home Phone:</td>
<td>Work Phone:</td>
<td>Cell Phone:</td>
</tr>
<tr><td><input name="text" type="text" id="homePhone" width="250" value="<?php echo $studentRow[9]; ?>"></td>
<td><input name="text" type="text" id="workPhone" width="250" value="<?php echo $studentRow[10]; ?>"></td>
<td><input name="text" type="text" id="cellPhone" width="250" value="<?php echo $studentRow[11]; ?>"></td>
</tr>
<tr><td>E-mail:</td></tr>
<tr><td><input name="text" type="text" id="email" width="250" value="<?php echo $studentRow[12]; ?>"></td></tr>

<tr><td><input type="Submit" value="Save Contact Information" id="contactInfo" style="width:15em "></td></tr>
</table>


<table width="800" align="center" style="margin-top:4em ">
<tr><td align="center" colspan="3"><b>Change Password</b></td></tr>
<tr><td width="200">Old Password:</td></tr>
<tr><td><input name="text" type="text" id="oldPassword" width="250"></td></tr>
<tr><td>New Password:</td></tr>
<tr>
<td><input name="text" type="text" id="newPassword" width="250"></td>
<td>
<?php 
	if(isset($passwordError))
	{
		echo $passwordError;
	}
?>
</td>
</tr>
<tr><td width="200">Repeat Password:</td></tr>
<tr><td><input name="text" type="text" id="repeatPassword" width="250"></td></tr>
<tr><td><input type="Submit" value="Change Password" id="changePassword" style="width:10em "></td></tr>
</form>
</table>
</body>
</html>
